package com.wh.concurrent.atomic;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class MethodTest {

	public static void main(String[] args) {
		final P1 p1 = new P1();
		final ReadWriteLock myLock = new ReentrantReadWriteLock();
		Thread t1 = new Thread() {
			@Override
			public void run() {
				while(true) {
					myLock.writeLock().lock();
					p1.set1();
					myLock.writeLock().unlock();
				}
			}
		};
		t1.start();
		
		Thread t2 = new Thread() {
			@Override
			public void run() {
				while(true) {
					myLock.writeLock().lock();
					p1.set2();
					myLock.writeLock().unlock();
				}
			}
		};
		t2.start();
		
		Thread t3 = new Thread() {
			@Override
			public void run() {
				while(true) {
					myLock.readLock().lock();
					p1.check();
					myLock.readLock().unlock();
				}
			}
		};
		t3.start();
	}
}
